﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LogisticsBLL;

namespace WebLosgic.TruckTeam
{
    public partial class CLxinxi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlname();
                //ddlsate();
                ddltype();
                BindTrucklist();
            }
        }

        public void BindTrucklist()
        {
            int pageIndex = AspNetPagerTool.CurrentPageIndex;
            int pageSize = AspNetPagerTool.PageSize;
            int cout = 0;
            string TruckName = DdlTruck.SelectedValue;
            string CarNumber = TextLeader.Text;
            string CarType = DDLtype.SelectedValue;
            string State = DDLstate.SelectedValue;
            DataTable dt = TruckBLL.BindCar(pageIndex,pageSize,out cout,CarNumber,TruckName,CarType,State);
            GvTruckList.DataSource = dt;
            AspNetPagerTool.RecordCount = cout;
            GvTruckList.DataBind();
        }

        protected void Btncha_Click(object sender, EventArgs e)
        {
            BindTrucklist();
        }


        /// <summary>
        /// 车队名字下拉列表
        /// </summary>
        public void ddlname()
        {
            TruckBLL tru = new TruckBLL();
            DataTable ta = tru.ddlbindCarName();
            DdlTruck.DataSource = ta;
            DdlTruck.DataTextField = "TeamName";
            DdlTruck.DataValueField= "TeamName";
            DdlTruck.DataBind();
            DdlTruck.Items.Insert(0, new ListItem("-请选择-","0"));
        }
         

        public void ddltype()
        {
            TruckBLL tru = new TruckBLL();
            DataTable ta = tru.ddlbindCarType();
            DDLtype.DataSource = ta;
            DDLtype.DataTextField = "Type";
            DDLtype.DataValueField = "Type";
            DDLtype.DataBind();
            DDLtype.Items.Insert(0, new ListItem("-请选择-","-1"));
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int rowIndex = ((GridViewRow)(((LinkButton)sender).NamingContainer)).RowIndex;
            string id = GvTruckList.DataKeys[rowIndex].Value.ToString();
            Response.Redirect("TruckInfo.aspx?tid=" + id);
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            int rowIndex = ((GridViewRow)(((LinkButton)sender).NamingContainer)).RowIndex;
            string id = GvTruckList.DataKeys[rowIndex].Value.ToString();
            Response.Redirect("UpdateTruck.aspx?tid=" + id);
        }

        protected void LbtnDel_Click(object sender, EventArgs e)
        {
            int rowIndex = ((GridViewRow)(((LinkButton)sender).NamingContainer)).RowIndex;
            int id =Convert.ToInt32(GvTruckList.DataKeys[rowIndex].Value.ToString());
            TruckBLL tub = new TruckBLL();
            int results = tub.deletTruck(id);
            if (results > 0)
            {
                string name = Session["names"].ToString();
                string Behavor = "" + name + ":删除车辆";
                int TypeId = 9;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = id.ToString();
                string ProcName = "View_TruckSel";
                HttpRequest request = HttpContext.Current.Request;
                string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result)) { result = request.ServerVariables["REMOTE_ADDR"]; }
                if (string.IsNullOrEmpty(result)) { result = request.UserHostAddress; }
                if (string.IsNullOrEmpty(result)) { result = "192.168.0.0"; }
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功')", true);
                }
                
            }
            BindTrucklist();
            
        }

        protected void AspNetPagerTool_PageChanged(object sender, EventArgs e)
        {
            BindTrucklist();
        }
    }
}